<?php
/**
 * Created by PhpStorm.
 * User: Victor
 * Date: 2018/9/12
 * Time: 14:32
 */

namespace app\manage\controller;


use think\Controller;
use think\Db;
use think\Request;
class Jiaoyi extends BaseController
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods:POST,GET");
        header("Access-Control-Allow-Headers:x-requested-with,content-type");
        header("Content-type:text/json;charset=utf-8");
    }
    public function index()
    {
        $data = input();
        if(empty($data['token'])){
            $result['code']=0;
            $result['msg']=urlencode('非法访问！');
            return urldecode(json_encode($result));
        }
        $name = base64_decode($data['token']);
        $token_check = db('guanli')
            ->where('bh',$name)
            ->find();
        if(count($token_check)>0){
            if(empty($data['zu_id']) or empty($data['lb'])){
                $jiaoyi_shuju = db('jiaoyi')
                    ->alias('j')
                    ->join('jiaoyi_zu z','j.zu_id = z.id','LEFT')
                    ->field('j.*,z.name')
                    ->order('j.zu_id,j.lb,j.shijian')
                    ->select();
            }else{
                $jiaoyi_shuju = db('jiaoyi')
                    ->alias('j')
                    ->join('jiaoyi_zu z','j.zu_id = z.id','LEFT')
                    ->field('j.*,z.name')
                    ->where('zu_id',$data['zu_id'])
                    ->where('lb',$data['lb'])
                    ->order('j.zu_id,j.lb,j.shijian')
                    ->select();
            }
            foreach ($jiaoyi_shuju as $key => $val){
                if(is_null($val['beizhu'])){
                    $jiaoyi[$key]['beizhu'] = '';
                }
                $jiaoyi_shuju[$key]['shijian'] = date('H:i:s', $val['shijian']);
                $jiaoyi_shuju[$key]['shijian1'] = date('H:i:s', $val['shijian1']);

//                if( $jiaoyi_shuju[$key]['kaipan_shijian'] > 0){
                    $jiaoyi_shuju[$key]['kaipan_shijian'] = date('H:i:s', $val['kaipan_shijian']);
//                }
//                if( $jiaoyi_shuju[$key]['shoupan_shijian'] > 0){
                    $jiaoyi_shuju[$key]['shoupan_shijian'] = date('H:i:s', $val['shoupan_shijian']);
//                }

                if($val['lb']==1){
                    $jiaoyi_shuju[$key]['lb_name'] = '开仓';
                }elseif ($val['lb']==2){
                    $jiaoyi_shuju[$key]['lb_name'] = '平仓';
                }else{
                    $jiaoyi_shuju[$key]['lb_name'] = '撤单';
                }
                if($val['tjia1_bz']==0){
                    $jiaoyi_shuju[$key]['tjia1_bz'] = '否';
                }else{
                    $jiaoyi_shuju[$key]['tjia1_bz'] = '是';
                }
            }
            $result['code']=1;
            $result['shuju']= $jiaoyi_shuju;
            $result['msg']=urlencode('请求交易数据成功');
            return urldecode(json_encode($result));
        }else{
            $result['code']=0;
            $result['msg']=urlencode('该用户不存在！');
            return urldecode(json_encode($result));
        }
    }
    public function jiaoyi_add_shuju()
    {
        $jiaoyi_zu = db('jiaoyi_zu')
            ->field('id,name')
            ->select();
        $lb = array(
        array("开仓",1),
        array("平仓",2),
        array("撤单",3));
        $result['code']=1;
        $result['name']= $jiaoyi_zu;
        $result['lb']= $lb;
        $result['msg']=urlencode('请求数据成功');
        return urldecode(json_encode($result));

    }
    public function jiaoyi_add_shijian(){
        $data=input();
        if(empty($data['zu_id'])){
            $result['code']=0;
            $result['msg']=urlencode('zu_id不能为空！');
            return urldecode(json_encode($result));
        }
        $where['zu_id']=$data['zu_id'];
        $jiaoyi=db('jiaoyi')
            ->where($where)
            ->select();
        if($jiaoyi){
            foreach($jiaoyi as $k=>$v){
                $jiaoyi[$k]['kaipan_shijian']=date('H:i:s',$v['kaipan_shijian']);
                $jiaoyi[$k]['shoupan_shijian']=date('H:i:s',$v['shoupan_shijian']);
            }
            $result['code']=1;
            $result['msg']=$jiaoyi;
            return urldecode(json_encode($result));
        }else{
            $result['code']=0;
            $result['msg']=urlencode('请求数据为空！');
            return urldecode(json_encode($result));
        }
    }
    public function jiaoyi_add()
    {
        $data = input();
        $data = collection($data)->toArray();
        if(empty($data['token'])){
            $result['code']=0;
            $result['msg']=urlencode('非法访问！');
            return urldecode(json_encode($result));
        }
        $name = base64_decode($data['token']);
        $token_check = db('guanli')
            ->where('bh',$name)
            ->find();
        if(count($token_check)>0){
            $detail = $data['shuju']['detail'];
            $data_detail = [];
            foreach ($detail as $key => $val){
                $shijian = '1980-11-20 '.$val['shijian'];
                $shijain1 = '1980-11-20 '.$val['shijian1'];
                $data_detail[$key]['zu_id'] =  $data['shuju']['name'];
                $data_detail[$key]['lb'] =  $data['shuju']['lb'];
                $data_detail[$key]['shijian'] =  strtotime($shijian);
                $data_detail[$key]['shijian1'] = strtotime($shijain1);
                $data_detail[$key]['tjia1_bz'] = $val['tjia1_bz'];
                $data_detail[$key]['beizhu'] = $val['beizhu'];

            }
            //dump($data_detail);die();
            $res = db('jiaoyi')
                ->insertAll($data_detail);
            if($res>0){
                $redis = $this -> redis;
                $re_name = 'update_bz';
                $re_value = '1';
                $redis->set($re_name,$re_value);
                $result['code']=1;
                $result['msg']=urlencode('提交成功');
                return urldecode(json_encode($result));
            }else{
                $result['code']=0;
                $result['msg']=urlencode('数据提交失败，请检查后再次提交！');
                return urldecode(json_encode($result));
            }
        }else{
            $result['code']=0;
            $result['msg']=urlencode('用户不存在！');
            return urldecode(json_encode($result));
        }

    }
    public function jiaoyi_delete()
    {
        $data = input();
        //dump($data);die();
        $data = collection($data)->toArray();
        if(empty($data['token']) or empty($data['zu_id']) or empty($data['lb'])){
            $result['code']=0;
            $result['msg']=urlencode('非法访问！');
            return urldecode(json_encode($result));
        }
        $name = base64_decode($data['token']);
        $token_check = db('guanli')
            ->where('bh',$name)
            ->find();
        if(count($token_check)>0){
            $res = db('jiaoyi')
                ->where('zu_id',$data['zu_id'])
                ->where('lb',$data['lb'])
                ->delete();
            if($res>0){
                $redis = $this -> redis;
                $re_name = 'update_bz';
                $re_value = '1';
                $redis->set($re_name,$re_value);
                $result['code']=1;
                $result['msg']=urlencode('删除成功');
                return urldecode(json_encode($result));
            }else{
                $result['code']=0;
                $result['msg']=urlencode('删除失败，请重试！');
                return urldecode(json_encode($result));
            }
        }else{
            $result['code']=0;
            $result['msg']=urlencode('用户不存在！');
            return urldecode(json_encode($result));
        }
    }
    public function jiaoyi_edit()
    {
        $data = input();
        $data = collection($data)->toArray();
        if(empty($data['token'])){
            $result['code']=0;
            $result['msg']=urlencode('非法访问！');
            return urldecode(json_encode($result));
        }
        $name = base64_decode($data['token']);
        $token_check = db('guanli')
            ->where('bh',$name)
            ->find();
        if(count($token_check)>0){
            $detail = $data['shuju']['detail'];
            $data_detail = [];
            foreach ($detail as $key => $val){
                $shijian = '1980-11-20 '.$val['shijian'];
                $shijain1 = '1980-11-20 '.$val['shijian1'];
                $data_detail[$key]['zu_id'] =  $data['shuju']['name'];
                $data_detail[$key]['lb'] =  $data['shuju']['lb'];
                $data_detail[$key]['shijian'] =  strtotime($shijian);
                $data_detail[$key]['shijian1'] = strtotime($shijain1);
                $data_detail[$key]['tjia1_bz'] = $val['tjia1_bz'];
                $data_detail[$key]['beizhu'] = $val['beizhu'];
                $data_detail[$key]['kaipan_shijian'] = strtotime('1980-11-20 '.$data['shuju']['kaipan_shijian']);
                $data_detail[$key]['shoupan_shijian'] = strtotime('1980-11-20 '.$data['shuju']['shoupan_shijian']);

            }
            Db::startTrans();
            try{
               $a =  db('jiaoyi')
                   ->where('zu_id',$data_detail[0]['zu_id'])
                   ->where('lb',$data_detail[0]['lb'])
                   ->delete();
                if ($a==0) {
                    throw new \Exception('修改失败:error');
                }
               $b = db('jiaoyi')->insertAll($data_detail);
                if (empty($b)) {
                    throw new \Exception('修改失败');
                }
                // 执行提交操作
                Db::commit();
                $redis = $this -> redis;
                $re_name = 'update_bz';
                $re_value = '1';
                $redis->set($re_name,$re_value);
                $result['code']=1;
                $result['msg']=urlencode('编辑成功');
                return urldecode(json_encode($result));
            }catch (\Exception $e){
                // 回滚事务
                Db::rollback();
                // 获取提示信息
                $result['code']=0;
                $result['msg']=urlencode($e->getMessage());
                return urldecode(json_encode($result));
            }
        }else{
            $result['code']=0;
            $result['msg']=urlencode('用户不存在！');
            return urldecode(json_encode($result));
        }
    }

}